﻿using Easy.Services.DbContexts;
using Easy.Services.Dtos;
using Easy.Services.Entities;
using Easy.Services.Interface;
using System.Linq.Expressions;

namespace Easy.Services.Core
{
    internal class InterService: IInterService
    {

        public long Add(Inter model)
        {
            using MySQLDbContext db = new();
            BaseService<Inter> baseService = new(db);
            return baseService.AddBase(model);

        }

        public List<Inter> GetList(Expression<Func<Inter, bool>> expression = null)
        {
            using MySQLDbContext db = new();
            BaseService<Inter> baseService = new(db);
            return baseService.GetList(expression);

        }

        public InterStatisticsDTO GetStatistics(Expression<Func<Inter, bool>> expression = null)
        {
            InterStatisticsDTO dto = new();
            int count = 11;
            DateTime time = DateTime.Now;
            DateTime startTime = DateTime.Now.AddMonths(-count).AddDays(-time.Day).Date;
            var queryData = this.GetList(i => i.CreaterDate > startTime);
            List<string> titles = ["product"];
            List<string> aData = ["增加"];
            List<string> dData = ["删除"];
            List<string> uData = ["修改"];
            List<string> qData = ["查询"];
            var queryGroup = queryData.GroupBy(q => q.CreaterDate.Value.ToString("yyyy-MM"));
            foreach (var item in queryGroup)
            {
                titles.Add(item.Key);
                aData.Add(item.Where(i => i.Type == 1).Count().ToString());
                dData.Add(item.Where(i => i.Type == 2).Count().ToString());
                uData.Add(item.Where(i => i.Type == 3).Count().ToString());
                qData.Add(item.Where(i => i.Type == 4).Count().ToString());
            }

            dto.Source = [[.. titles], [.. aData], [.. dData], [.. uData], [.. qData]];
            dto.SeriesTypes = [];
            for (int i = 0; i <= queryGroup.Count(); i++)
            {
                dto.SeriesTypes.Add(new() { Type = "bar" });
            }

            dto.RoseLegendData = ["增加", "删除", "修改", "查询"];
            queryData.Where(i => i.Type == 1).Count().ToString();
            dto.SeriesRadiusData =
            [
                new() { Value= queryData.Where(i => i.Type == 1).Count().ToString(), Name= "增加" },
                new() { Value= queryData.Where(i => i.Type == 2).Count().ToString(), Name= "删除" },
                new() { Value= queryData.Where(i => i.Type == 3).Count().ToString(), Name= "修改" },
                new() { Value= queryData.Where(i => i.Type == 4).Count().ToString(), Name= "查询" }
            ];
            dto.SeriesAreaData = dto.SeriesRadiusData;
            return dto;
        }

        public long GetTotal(Expression<Func<Inter, bool>> whereExpression = null, bool includeDelete = false)
        {
            using MySQLDbContext db = new();
            BaseService<Inter> baseService = new(db);
            return baseService.GetTotalCount(whereExpression, includeDelete);

        }

    }
}
